package com.example.demo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.net.URL;

import org.assertj.core.internal.InputStreams;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;

public class UploadLocalFile {

	public static boolean upload(String filePath) {
		boolean status = true;
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.autoZone());
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// ...生成上传凭证，然后准备上传
		String accessKey = Config.AK;
		String secretKey = Config.SK;
		String bucket = Config.bucketName;
		// 如果是Windows情况下，格式是 D:\\qiniu\\test.png
		String localFilePath = filePath;
		// 默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = null;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
			Response response = uploadManager.put(localFilePath, key, upToken);
			// 解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			System.out.println(putRet.key);
			System.out.println(putRet.hash);
		} catch (QiniuException ex) {
			Response r = ex.response;
			System.err.println(r.toString());
			try {
				System.err.println(r.bodyString());
			} catch (QiniuException ex2) {
				ex2.printStackTrace();
				status = false;
				;
			}
		}
		return status;
	}

	public static boolean uploadUrl(String url) throws IOException {
		
			
		boolean status = true;
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.autoZone());
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// ...生成上传凭证，然后准备上传
		String accessKey = Config.AK;
		String secretKey = Config.SK;
		String bucket = Config.bucketName;
		// 默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = null;
		try {
			
			Auth auth = Auth.create(accessKey, secretKey);
			String upToken = auth.uploadToken(bucket);
			try {
				Response response = uploadManager.put(getBytesByUrl(url).toByteArray(), key, upToken);
				
				// 解析上传成功的结果
				DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
				
				System.out.println(putRet.key);
				System.out.println(putRet.hash);
			} catch (QiniuException ex) {
				Response r = ex.response;
				System.err.println(r.toString());
				try {
					System.err.println(r.bodyString());
				} catch (QiniuException ex2) {
					// ignore
					status = false;

				}
			}
		} catch (UnsupportedEncodingException ex) {
			// ignore
			ex.printStackTrace();
			status = false;
		}
		return status;

	}
	
	
	public static boolean uploadUrlByProxy(String url) throws IOException {
		Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("",9010) );
		
		boolean status = true;
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.autoZone());
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		// ...生成上传凭证，然后准备上传
		String accessKey = Config.AK;
		String secretKey = Config.SK;
		String bucket = Config.bucketName;
		// 默认不指定key的情况下，以文件内容的hash值作为文件名
		String key = null;
		try {
			
			Auth auth = Auth.create(accessKey, secretKey);
			String upToken = auth.uploadToken(bucket);
			try {
				Response response = uploadManager.put(getBytesByUrl(url).toByteArray(), key, upToken);
				
				// 解析上传成功的结果
				DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
				
				System.out.println(putRet.key);
				System.out.println(putRet.hash);
			} catch (QiniuException ex) {
				Response r = ex.response;
				System.err.println(r.toString());
				try {
					System.err.println(r.bodyString());
				} catch (QiniuException ex2) {
					// ignore
					status = false;

				}
			}
		} catch (UnsupportedEncodingException ex) {
			// ignore
			ex.printStackTrace();
			status = false;
		}
		return status;

	}


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String path = "C:/Users/Administrator/Desktop/89777a33-abc7-4a68-972d-4e55875ccc00.jpg";
		// upload(path);
		String picUrl = "https://img1.doubanio.com/view/photo/lphoto/public/p2469763877.webp";
		try {
			uploadUrl(picUrl);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static ByteArrayOutputStream getBytesByUrl(String picurl) throws IOException {
		URL url = new URL(picurl);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		InputStream is = null;
		try {
			is = url.openStream();
			byte[] byteChunk = new byte[4096]; // Or whatever size you want to
												// read in at a time.
			int n;

			while ((n = is.read(byteChunk)) > 0) {
				baos.write(byteChunk, 0, n);
			}
		} catch (IOException e) {
			System.err.printf("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
			e.printStackTrace();
			// Perform any other exception handling that's appropriate.
		} finally {
			if (is != null) {
				is.close();
			}
		}
return baos ;
	}
}
